package com.bwie.service.imp;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.es.EsVisitor;
import com.bwie.mapper.ListMapper;
import com.bwie.pojo.People;
import com.bwie.service.PeopleService;
import com.bwie.unit.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PeopleServiceImpl extends ServiceImpl<ListMapper, People> implements PeopleService {

    @Autowired
    private ListMapper listMapper;

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Override
    public List<People> peopleList(Integer uid, Integer rid) {
        return listMapper.peopleList(uid,rid);
    }

    @Override
    public R createEs() {
        IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(EsVisitor.class);
        boolean exists = indexOperations.exists();
        if (exists){
            indexOperations.delete();
        }
        indexOperations.create();
        Document mapping = indexOperations.createMapping();
        indexOperations.putMapping(mapping);
        return R.OK();
    }
}
